package com.webex.ta.hydra.core;

import java.util.List;
import java.util.ArrayList;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-19
 * Time: 13:07:18
 */
public class TestResult {
    private static final AssertionResult[] EMPTY_AR = new AssertionResult[0];

    private boolean success;
    private boolean error;
    private boolean failure;

    private String keyword;
    private String[] args;
    private String request;
    private String requestHeaders = "";
    private String response;
    private String responseHeaders = "";
    private String responseCode = "";
    private long startTime;
    private long endTime;
    private Command creator;
    private List<AssertionResult> assertionResults;

    private TestResult() {
        success = true;
        error = false;
        failure = false;
    }

    public static TestResult createInstance(Command creator) {
        TestResult result = new TestResult();
        result.setStartTime(System.currentTimeMillis());
        result.setCreator(creator);
        if (creator != null)
            creator.addTestResult(result);
        return result;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean v) {
        if (v) {
            success = true;
            error = false;
            failure = false;
        } else {
            success = false;
        }
    }

    public boolean isError() {
        return error;
    }

    public void setError(boolean value) {
        error = value;
        if (error) {
            success = false;
        }
    }

    public boolean isFailure() {
        return failure;
    }

    public void setFailure(boolean value) {
        failure = value;
        if (failure)
            success = false;
    }

    public long getEndTime() {
        return endTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    public String getRequest() {
        return request;
    }

    public void setRequest(String request) {
        this.request = request;
    }

    public String getRequestHeaders() {
        return requestHeaders;
    }

    public void setRequestHeaders(String requestHeaders) {
        this.requestHeaders = requestHeaders;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    public String getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(String responseCode) {
        this.responseCode = responseCode;
    }

    public String getResponseHeaders() {
        return responseHeaders;
    }

    public void setResponseHeaders(String responseHeaders) {
        this.responseHeaders = responseHeaders;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public Command getCreator() {
        return creator;
    }

    public void setCreator(Command creator) {
        this.creator = creator;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String[] getArguments() {
        return args;
    }

    public void setArguments(String[] args) {
        this.args = args;
    }

    public void addAssertionResult(AssertionResult ar) {
        if (assertionResults == null)
            assertionResults = new ArrayList<AssertionResult>();
        assertionResults.add(ar);
    }

    public AssertionResult[] getAssertionResults() {
        if (assertionResults == null) {
			return EMPTY_AR;
		}
		return assertionResults.toArray(new AssertionResult[assertionResults.size()]);
    }


    @Override
    public String toString() {
        return printParametersGracefully(args);
    }

    public String prettyFormat() {
        StringBuffer sb = new StringBuffer();
        sb.append(keyword).append(printParametersGracefully(args));
        return sb.toString();
    }

    protected String printParametersGracefully(String[] args) {
        if (args == null || args.length == 0) {
            return "()";
        }
        StringBuffer buf = new StringBuffer();
        buf.append("(");
        for (int i = 0; i < args.length - 1; i++) {
            buf.append(args[i]).append(",");
        }
        buf.append(args[args.length - 1]);
        buf.append(")");
        return buf.toString();
    }
}
